<template>
  <div class="app-container">
    <el-form ref="form"
             :model="form"
             :rules="rules"
             size="small"
             label-width="150px">
      <el-form-item label="项目编码">
        <el-input v-model="form.projectCode"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="项目名称">
        <el-input v-model="form.projectName"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="集团名称">
        <el-input v-model="form.groupName"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="集团编码">
        <el-input v-model="form.groupCode"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="区域">
        <el-select v-model="form.area"
                   filterable
                   placeholder="请选择">
          <el-option v-for="item in dict.ict_project_attributive"
                     :key="item.id"
                     :label="item.label"
                     :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="新办事处">
        <el-input v-model="form.newOffice"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="项目经理">
        <el-input v-model="form.projectManager"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="客户经理">
        <el-input v-model="form.customerManager"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="ESOP登记ICT出账收入（万元）-含税">
        <el-input v-model="form.esopIctOutcomeTax"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="ESOP登记ICT出账收入（万元）-不含税">
        <el-input v-model="form.esopIctOutcomeNoTax"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="ESOP登记出账审核完成时间">
        <el-date-picker v-model="form.esopOutcomeCheckTime"
                        type="datetime"
                        style="width: 370px;" />
      </el-form-item>
      <el-form-item label="产品编码">
        <el-input v-model="form.productCode"
                  style="width: 370px;" />
      </el-form-item>
      <el-form-item label="项目行业类型">
        <el-select v-model="form.industryType"
                   filterable
                   placeholder="请选择">
          <el-option v-for="item in dict.ict_project_industry_type"
                     :key="item.id"
                     :label="item.label"
                     :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="费项名称">
        <el-select v-model="form.costName"
                   filterable
                   placeholder="请选择">
          <el-option v-for="item in dict.ict_cost_type"
                     :key="item.id"
                     :label="item.label"
                     :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="info"
                   @click="cancelForm()">取消</el-button>
        <el-button :loading="loading"
                   type="primary"
                   @click="submitForm(form)">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</>

<script>
import crudIctStandingBook from '@/api/ict/ictStandingBook/ictStandingBook'
import CRUD from '@crud/crud'
const defaultForm = { id: null, projectCode: null, projectName: null, groupName: null, groupCode: null, area: null, newOffice: null, projectManager: null, customerManager: null, esopIctOutcomeTax: null, esopIctOutcomeNoTax: null, esopOutcomeCheckTime: null, productCode: null, industryType: null, costName: null, delFlag: null, updateTime: null, createName: null }
export default {
  name: 'IctStandingBookForm',
  dicts: ['ict_project_attributive', 'ict_project_industry_type', 'ict_cost_type', 'del_flag'],
  data() {
    return {
      loading: false,
      form: {},
      id: this.$route.query.id,
      permission: {
        add: ['admin', 'ictManager', 'ictIncomeBase:add'],
        edit: ['admin', 'ictManager', 'ictIncomeBase:edit'],
        del: ['admin', 'ictManager', 'ictIncomeBase:del']
      },
      rules: {
        id: [
          { required: true, message: '唯一id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.id) {
      this.getDetail(this.id)
    } else {
      this.initForm()
    }
  },
  methods: {
    cancelForm() {
      this.$router.go(-1)
    },
    submitForm(form) {
      this.loading = true
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.id) {
            this.toEdit(form)
          } else {
            this.toAdd(form)
          }
        } else {
          console.log('error submit!')
          return false
        }
      })
      this.loading = false
    },
    toAdd(form) {
      crudIctStandingBook.add(form).then(res => {
        if (res.msg === 'OK') {
          this.$notify.success({
            title: '添加成功'
          })
          this.$router.go(-1)
        }
      }).catch(e => {
        this.$notify.error({
          title: '添加失败' + e
        })
      })
    },
    toEdit(form) {
      crudIctStandingBook.edit(form).then(res => {
        if (res.msg === 'OK') {
          this.loading = false
          this.$notify.success({
            title: '更新成功'
          })
          this.$router.go(-1)
        }
      }).catch(e => {
        this.$notify.error({
          title: '更新失败' + e
        })
      })
    },
    getDetail(id) {
      crudIctStandingBook.get(id).then(res => {
        this.form = res.data
      }).catch(e => {
        console.log(e)
      })
    },
    initForm() {
      this.form = defaultForm
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 获取数据后对数据进行二次处理
    [CRUD.HOOK.afterRefresh]() {

    }
  }

}
</script>

<style lang="scss" scoped>
</style>
